import { ref } from 'vue';
import { message } from 'ant-design-vue';
import { getJudgeResult, getJudgeDetailedResult } from '@/api/hoj-judge';

export function useJudgeResult() {
  const submission = ref(null);
  const judgeCaseList = ref([]);
  const loading = ref(false);


  // 获取评测结果
  const fetchJudgeResult = async (submitId) => {
    try {
      loading.value = true;
      const response = await getJudgeResult(submitId);
      const detailedResponse = await getJudgeDetailedResult(submitId);
      submission.value = response.data.submission;
      judgeCaseList.value = detailedResponse.data.judgeCaseList;
    } catch (error) {
      message.error('获取评测结果失败');
      console.error('获取评测结果失败:', error);
    } finally {
      loading.value = false;
    }
  };

  // 重新评测
  const handleReJudge = async () => {
    if (!submission.value) return;
    
    try {
      loading.value = true;
      message.loading('正在重新评测...', 0);
      const response = await reJudge(submission.value.submitId);
      message.destroy(); // 清除loading消息
      
      if (response.data.success) {
        message.success('重新评测成功');
        // 重新获取评测结果
        await fetchJudgeResult(submission.value.submitId);
      } else {
        message.error(response.data.message || '重新评测失败');
      }
    } catch (error) {
      message.destroy(); // 清除loading消息
      message.error('重新评测失败');
      console.error('重新评测失败:', error);
    } finally {
      loading.value = false;
    }
  };

  return {
    submission,
    judgeCaseList,
    loading,
    fetchJudgeResult,
    handleReJudge
  };
} 